/*
 * Copyright 2011 Sina.
 *
 * Licensed under the Apache License and Weibo License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.open.weibo.com
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cuit.jifver.weibo.data;

import java.io.IOException;
import java.net.MalformedURLException;

import com.cuit.jifver.weibo.inter.MyWeibo;
import com.cuit.jifver.weibo.net.AccessToken;
import com.cuit.jifver.weibo.net.AccessTokenHeader;
import com.cuit.jifver.weibo.net.RequestHeader;
import com.cuit.jifver.weibo.net.RequestToken;
import com.cuit.jifver.weibo.net.RequestTokenHeader;
import com.cuit.jifver.weibo.net.Utility;
import com.cuit.jifver.weibo.util.Constants;
//import com.cuit.jifver.weibo.net.XAuthHeader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

/**
 * Encapsulation main Weibo APIs, Include: 1. getRquestToken , 2.
 * getAccessToken, 3. url request. Used as a single instance class. Implements a
 * weibo api as a synchronized way. 默认为sina微薄
 * 
 * @author 朱勇
 */
public class Weibo {

	public static String SERVER = "http://api.t.sina.com.cn/";
	public static String URL_OAUTH_TOKEN = "http://api.t.sina.com.cn/oauth/request_token";
	public static String URL_AUTHORIZE = "http://api.t.sina.com.cn/oauth/authorize";
	public static String URL_ACCESS_TOKEN = "http://api.t.sina.com.cn/oauth/access_token";
	//public static String URL_AUTHENTICATION = "http://api.t.sina.com.cn/oauth/authenticate";

	public static String APP_KEY = "1108672235";
	public static String APP_SECRET = "417466d2685f80420e60e8cec068fcec";
	
	private static Weibo mWeiboInstance = null;
	private AccessToken mAccessToken = null;
	private RequestToken mRequestToken = null;
	
	private MyWeibo myWeibo = null;

	private Weibo() {
		Utility.setRequestHeader("Accept-Encoding", "gzip");
		Utility.setTokenObject(this.mRequestToken);
	}

	public static Weibo getInstance() {
		if (mWeiboInstance == null) {
			mWeiboInstance = new Weibo();
		}
		return mWeiboInstance;
	}

	// 设置accessToken
	public void setAccessToken(AccessToken token) {
		mAccessToken = token;
	}

	public AccessToken getAccessToken() {
		return this.mAccessToken;
	}

	public MyWeibo getMyWeibo()
	{
		return myWeibo;
	}

	public void setMyWeibo(MyWeibo myWeibo)
	{
		this.myWeibo = myWeibo;
	}

	public void setupConsumerConfig(String consumer_key, String consumer_secret) {
		Weibo.APP_KEY = consumer_key;
		Weibo.APP_SECRET = consumer_secret;
	}

	public void setupUrlConfig(String server, String url_oauth_token, String url_authorize, String url_access_token) {
		Weibo.SERVER = server;
		Weibo.URL_OAUTH_TOKEN = url_oauth_token;
		Weibo.URL_AUTHORIZE = url_authorize;
		Weibo.URL_ACCESS_TOKEN = url_access_token;
		//Weibo.URL_AUTHENTICATION = url_authentication;
	}

	public void setRequestToken(RequestToken token) {
		this.mRequestToken = token;
	}

	// 设置oauth_verifier
	public void addOauthverifier(String verifier) {
		Log.e("", "mRequestToken:" + (mRequestToken == null));
		mRequestToken.setVerifier(verifier);
	}

	/**
	 * Requst sina weibo open api by get or post
	 * 
	 * @param url
	 *            Openapi request URL.
	 * @param params
	 *            http get or post parameters . e.g.
	 *            gettimeling?max=max_id&min=min_id max and max_id is a pair of
	 *            key and value for params, also the min and min_id
	 * @param httpMethod
	 *            http verb: e.g. "GET", "POST", "DELETE"
	 * @throws IOException
	 * @throws MalformedURLException
	 * @throws WeiboException
	 */
	public String request(Context context, String url, WeiboParameters params, String httpMethod, AccessToken token) throws WeiboException {
		Utility.setAuthorization(new RequestHeader());
		String rlt = Utility.openUrl(context, url, httpMethod, params, this.mAccessToken);
		return rlt;
	}

	/**/
	public RequestToken getRequestToken(Context context, String key, String secret, String callback_url) throws WeiboException {
		Utility.setAuthorization(new RequestTokenHeader());
		WeiboParameters postParams = new WeiboParameters();
		postParams.add("oauth_callback", callback_url);
		String rlt;
		rlt = Utility.openUrl(context, Weibo.URL_OAUTH_TOKEN, "GET", postParams, null);
		RequestToken request = new RequestToken(rlt);
		this.mRequestToken = request;
		Log.e("", "RequestToken:"+request.getToken());
		Log.e("", "RequestSecret:"+request.getSecret());
		return request;
	}

	public AccessToken generateAccessToken(Context context, RequestToken requestToken) throws WeiboException {
		Utility.setAuthorization(new AccessTokenHeader());
		WeiboParameters authParam = new WeiboParameters();
		authParam.add("oauth_verifier", this.mRequestToken.getVerifier()/* "605835" */);
		//authParam.add("source", APP_KEY);
		System.out.println("xiao_r="+this.mRequestToken);
		System.out.println("xiao_a="+this.mAccessToken);
		String rlt = Utility.openUrl(context, Weibo.URL_ACCESS_TOKEN, "GET", authParam, this.mRequestToken);
		AccessToken accessToken = new AccessToken(rlt);
		this.mAccessToken = accessToken;
		Log.e("", "mAccessToken:"+mAccessToken.getToken());
		return accessToken;
	}

/*	public AccessToken getXauthAccessToken(Context context, String app_key, String app_secret, String usrname, String password)
			throws WeiboException {
		Utility.setAuthorization(new XAuthHeader());
		WeiboParameters postParams = new WeiboParameters();
		postParams.add("x_auth_username", usrname);
		postParams.add("x_auth_password", password);
		postParams.add("oauth_consumer_key", APP_KEY);
		String rlt = Utility.openUrl(context, Weibo.URL_ACCESS_TOKEN, "POST", postParams, null);
		AccessToken accessToken = new AccessToken(rlt);
		this.mAccessToken = accessToken;
		return accessToken;
	}*/

	/**
	 * Share text content or image to weibo .
	 * 
	 */
	public boolean share2weibo(Activity activity, String accessToken, String tokenSecret, String content, String picPath)
			throws WeiboException {
		if (TextUtils.isEmpty(accessToken)) {
			throw new WeiboException("token can not be null!");
		}
		else if (TextUtils.isEmpty(tokenSecret)) {
			throw new WeiboException("secret can not be null!");
		}

		if (TextUtils.isEmpty(content) && TextUtils.isEmpty(picPath)) {
			throw new WeiboException("weibo content can not be null!");
		}
		// Intent i = new Intent(activity, ShareActivity.class);
		// i.putExtra(ShareActivity.EXTRA_ACCESS_TOKEN, accessToken);
		// i.putExtra(ShareActivity.EXTRA_TOKEN_SECRET, tokenSecret);
		// i.putExtra(ShareActivity.EXTRA_WEIBO_CONTENT, content);
		// //i.putExtra(ShareActivity.EXTRA_PIC_URI, picPath);
		// activity.startActivity(i);

		return true;
	}

	private void startActivitySignOn(Activity activity, String key, String secret) {

	}

	private void startDialogAuth(Activity activity, String app_key, String app_secret) {

	}

	public void authorizeCallBack(int requestCode, int resultCode, Intent data) {

	}

}
